<template>
  <div>
    <el-card shadow="never" class="margin-30">
      <div>
        <div class="common-title">
          <h4>退单信息
            <!-- 0->待处理；1->退货中；2->已完成；3->已拒绝 -->
            <el-tag v-if="returnDetail.status==0" type="danger" size="mini" effect="dark">待处理</el-tag>
            <el-tag v-if="returnDetail.status==1" type="info" size="mini" effect="dark">退货中</el-tag>
            <el-tag v-if="returnDetail.status==2" type="success" size="mini" effect="dark">已完成</el-tag>
            <el-tag v-if="returnDetail.status==3" type="warning" size="mini" effect="dark">已拒绝</el-tag>

          </h4>
        </div>
        <div>
          <el-row :gutter="20">
            <el-col :span="6" :offset="0">
              <!-- <p>订单编号:{{ returnDetail.orderSn }} <el-button type="text" style="color:#409eff" size="mini" @click="goOrderDetail">查看订单详情</el-button>
              </p> -->
              <p>订单编号:{{ returnDetail.orderSn }} <router-link :to="{ name: 'OrderDetail', params: { id: returnDetail.orderId }}">查看订单详情</router-link>
              </p>
            </el-col>
            <el-col :span="6" :offset="0">
              <p>退单人姓名:{{ returnDetail.returnName }}</p>
            </el-col>
            <el-col :span="6" :offset="0">
              <el-col :span="6" :offset="0" />
            </el-col></el-row>

        </div>
      </div>

      <div>
        <div class="common-title">
          <h4>收货人信息:</h4>
        </div>
      </div>
      <div>
        <div class="common-title">
          <h4>商品信息:</h4>
        </div>
        <el-table :data="[returnDetail]" border stripe>
          <el-table-column
            prop="productName"
            align="center"
            label="商品名称"
          />
          <el-table-column

            align="center"
            label="商品图片"
          >
            <template slot-scope="scope">
              <img width="100" height="100" :src="scope.row.productPic" alt="">
            </template>
          </el-table-column>

        </el-table>

      </div>

      <div>
        <div class="common-title">
          <h4>费用信息:</h4>
        </div>
        <div v-if="returnDetail.status==0">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">
            <el-row :gutter="20">
              <el-col :span="6" :offset="0">
                <el-form-item label="退款金额" prop="returnAmount">
                  <el-input-number v-model="form.returnAmount" />
                </el-form-item>
              </el-col>
              <el-col :span="6" :offset="0">
                <el-form-item label="处理人" prop="handleMan">
                  <el-input v-model="form.handleMan" />
                </el-form-item>
              </el-col>
              <el-col :span="6" :offset="0">
                <el-form-item label="处理备注" prop="handleNote">
                  <el-input v-model="form.handleNote" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item>
              <el-button type="primary" @click="doAgree">同意</el-button>
              <el-button>拒绝</el-button>
            </el-form-item>
          </el-form>

        </div>
        <div v-if="returnDetail.status==1">
          <!-- 处理结果: -->
          <el-form ref="recevieFrom" :model="recevieFrom" :rules="recevieRules" label-width="80px" size="mini">
            <el-row :gutter="20">
              <el-col :span="6" :offset="0">
                <el-form-item label="收货人" prop="receiveMan">
                  <el-input v-model="recevieFrom.receiveMan" />
                </el-form-item>
              </el-col>
              <el-col :span="6" :offset="0">
                <el-form-item label="收货备注" prop="receiveNote">
                  <el-input v-model="recevieFrom.receiveNote" />
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item>
              <el-button type="primary" @click="doRecevieProduct">确认收货</el-button>

            </el-form-item>
          </el-form>

        </div>
        <div v-if="returnDetail.status==2">
          <!-- 收货信息: -->
          收货信息:
        </div>
      </div>

    </el-card>

  </div>
</template>
<script>
import {
  orderReturn as orderReturnApi,
  agreeApply as agreeApplyApi,
  receiveProduct as receiveProductApi
} from '@/api/order/returnOrder'
export default {
  name: 'ReturnDetail',
  mixins: [],
  data() {
    return {
      returnOrderId: '',
      returnDetail: {},
      form: {},
      recevieFrom: {},
      rules: {
        returnAmount: [{ required: true, message: '退款金额必填', trigger: 'blur' }],
        handleMan: [{ required: true, message: '处理人必填', trigger: 'blur' }],
        handleNote: [{ required: true, message: '处理备注必填', trigger: 'blur' }]
      },
      recevieRules: {
        receiveMan: [{ required: true, message: '退款金额必填', trigger: 'blur' }],
        receiveNote: [{ required: true, message: '退款金额必填', trigger: 'blur' }]
      }
    }
  },
  computed: {
  },
  watch: {
  },
  created() {
    this.returnOrderId = this.$route.params.id
    this.getReturnOrderDetail()
  },
  mounted() {
  },
  methods: {
    doRecevieProduct() {
      this.$refs.recevieFrom.validate(async(valid) => {
        if (valid) {
          const res = await receiveProductApi(this.returnOrderId, {
            id: this.returnOrderId,
            ...this.recevieFrom
          })

          const { success, message } = res
          if (success) {
            this.getReturnOrderDetail()
          } else {
            this.$message.error(message)
          }
        }
      })
    },
    doAgree() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          const res = await agreeApplyApi(this.returnOrderId, {
            id: this.returnOrderId,
            ...this.form
          })

          const { success, message } = res
          if (success) {
            this.getReturnOrderDetail()
          } else {
            this.$message.error(message)
          }
        }
      })
    },
    goOrderDetail() {
      this.$router.push({ name: 'OrderDetail', params: { id: this.returnDetail.orderId }})
    },
    async getReturnOrderDetail() {
      const res = await orderReturnApi(this.returnOrderId)
      const { success, data, message } = res
      if (success) {
        this.returnDetail = data.orderReturnApply
      } else {
        this.$message.error(message)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
